package com.navinfo.tripanalysis.offline.service.impl;


import com.navinfo.tripanalysis.common.pojo.PointProtocol;
import com.navinfo.tripanalysis.common.pojo.ProtocolEnum;
import com.navinfo.tripanalysis.offline.service.DataCleaningJudgeTripStatusService;
import com.navinfo.tripanalysis.offline.service.PreTripInfoDataCleaningService;
import com.navinfo.tripanalysis.offline.service.DataCleaningUpdAbnormalPointService;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据清洗：行程切分实现类
 */
@Data
public class PreTripInfoDataCleaningServiceImpl implements PreTripInfoDataCleaningService, Serializable {

    private DataCleaningJudgeTripStatusService dataCleaningJudgeTripStatusService;
    private DataCleaningUpdAbnormalPointService dataCleaningUpdAbnormalPointService;
    private double Speed_Max_Threshold;
    private int Rotation_Max_Threshold;

    @Override
    public List<PointProtocol> preJudgeTrip(List<PointProtocol> points) {
        //过滤最后一个0200点之后的数据
        for(int i = points.size() - 1;i>=0;i--){
            if(points.get(i).getProtocol() == ProtocolEnum.LOCATION){
                break;
            }
            points.remove(i);
        }
        //初始化行程id
        int tripId = 0;
        //初始化状态
        int status = -1;
        //上一个点gpstime
        Long lastgpstime = null;
        //当前点gpstime
        Long gpstime = null;
        //上一个0200点
        PointProtocol pre = null;
        //初始化结果集合
        List<PointProtocol> resultList = new ArrayList<>();
        //遍历位置点
        for(PointProtocol p:points){
            //if(p.getGpsTime() >= endtime && p.getProtocol()==1){break;}
            //不处理第一个0200点之前的数据
            if(status == -1&& p.getProtocol()==ProtocolEnum.REALTIME){continue;}
            //当前点gpstime
            gpstime = p.getGpsTime();
            //处理过程
            if(ProtocolEnum.LOCATION.equals(p.getProtocol())){
                //判断行车状态
                //logger.info("point:lastgpstime:{},speed:{},acc:{},tripid:{}:status:{}",lastgpstime,p.getSpeedKmh(),p.getAccStatus(),tripId,status);
                status = dataCleaningJudgeTripStatusService.judgeTripStatus(lastgpstime,gpstime,p);
                switch (status){
                    case TRIP_START:p.setTripId(++tripId);resultList.add(p);lastgpstime = gpstime; break;
                    case TRIP_IN:p.setTripId(tripId);resultList.add(p);lastgpstime = gpstime;break;
                    case TRIP_END:p.setTripId(tripId);resultList.add(p);lastgpstime = null;break;
                    case TRIP_FORCE_END:p.setTripId(tripId);resultList.add(p);lastgpstime = null;break;
                    case TRIP_NO_START:break;
                    default: break;
                }
                pre = p;
            }else{
                //logger.info("gpstime:{},dataUpd:{},speed:{},status:{}",p.getGpsTime(),p.getDataUpd(),p.getSpeedKmh(),status);
                switch (status){
                    case TRIP_START:
                    case TRIP_IN:p.setTripId(tripId);resultList.add(dataCleaningUpdAbnormalPointService.DataUpdBy0F37(pre,p,Speed_Max_Threshold,Rotation_Max_Threshold));break;
                    default: break;
                }
            }
        }
        //logger.info("result1.count:{}",resultList.size());
        return resultList;
    }
}
